package com.cloudptt.api.product.func.call.group.fsm.call;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.ISecondChange;
import com.cloudptt.api.product.func.call.TimerMeter;
import com.cloudptt.api.product.log.Log;

public class ActiviteState implements ICallState,ISecondChange
{
	private GroupCallAgent agent = null;
	private TimerMeter timerMeter = null;
	public ActiviteState(GroupCallAgent agent)
	{
		this.agent = agent;
		this.timerMeter = new TimerMeter(this);
		this.timerMeter.startTiming();
	}

	public int doUserAction(int btnAction)
	{
		if(agent == null)
		{
			return CauseCont.OperateErr_NoAgent;
		}
		if(CallConst.BtnAction_Hangup == btnAction)
		{
			int result = this.agent.doHangupAction(603);//.doExitAction();
			if(CauseCont.OperateCheck_Suc == result)
			{
//				gotoDisconnectingState();
				this.agent.goToState(CallConst.CallState_DisconnectingState);
			}
			return result;
		}
		else if(CallConst.BtnAction_Subscribe == btnAction)
		{
			int result = this.agent.doSubscribe();
			if(CauseCont.OperateCheck_Suc == result)
			{
				this.agent.goToState(CallConst.CallState_DisconnectingState);
			}
			return result;
		}
		else if(CallConst.BtnAction_SwitchCamera == btnAction)
		{
			int result = this.agent.doSwitchCamera();
			return result;
		}
		return CauseCont.OperateErr_Invalid;
	}
	public void receiveCallAction(int talkAction)
	{
		if(agent == null)
		{
			return;
		}
		if(CallConst.Session_Disconnected == talkAction)
		{
			this.agent.goToState(CallConst.CallState_InitState);
//			gotoInitialState();
		}
	}
	
	public int getState()
	{
		return CallConst.CallState_Activite;
	}
	public void destory()
	{
		if(timerMeter != null)
		{
			timerMeter.stopTiming();
			timerMeter = null;
		}
		agent = null;
	}
	
	@Override
	public void secondChange(int seconds) {
		// TODO Auto-generated method stub
		if(agent != null)
		{
			agent.updateCallDuration(seconds);
		}
	}
}
